package com.neuedu.corejava.jdbc1;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;

/**
 * 20190809
 * 设计一个jdbc操作工具类，用于封装通用操作
 * @author admin
 *
 */
public class DBHelper {
	//封装4个操作
	private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
	private static final String URL="jdbc:oracle:thin://localhost:1521/orcl";
	private static final String USER="jdbcuser";
	private static final String PASSWORD="123456";
	
	//1.注册驱动2.建立连接
	public  static Connection openConnection(){
		Connection conn=null;
		
		try {
			Class.forName(DRIVER);
			conn=DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return conn;
	}
	
	
	
	//6.关闭连接，释放资源
	public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
		//这是一个标准的关闭动作
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}
	}
	

}
